package yxy.game.pm2.command.arena.toparena;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ArenaMessage;
import yxy.game.pm2.module.arena.toparena.TopAreanHandle;
import yxy.game.pm2.module.arena.toparena.TopAreanModule;
import yxy.game.pm2.module.arena.toparena.TopAreanPlayer;
import yxy.game.pm2.module.arena.toparena.TopAreanStructure;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.HashMap;

/**
 * 巅峰赛详情
 *
 * @ClassName CMD_TopArenaInfo
 * @Author jingtao-zheng
 * @Date 2022/9/8 17:06
 * @Version 1.0
 **/
@MessageType(proto = Command.TopArenaInfo, classtype = ArenaMessage.TopArenaInfoRequest.class)
public class CMD_TopArenaInfo extends Command<ArenaMessage.TopArenaInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, ArenaMessage.TopArenaInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int group = request.getGroup();//组
        ArenaMessage.TopArenaInfoResponse.Builder response = ArenaMessage.TopArenaInfoResponse.newBuilder();
        try {
            PlayerBean playerBean = PlayerBean.get(playerCode);
            int serverId = playerBean.getServerId();
            ServerBean serverBean = ServerBean.get(serverId);
            int areaId = serverBean.getAreaId();
            TopAreanModule topAreanModule = TopAreanModule.get(areaId);
            HashMap<TopAreanHandle.TopAreanCode, TopAreanStructure> mapStructure = topAreanModule.matchGroup(group);
            TopAreanHandle.TopAreanCode[] finalCodes = new TopAreanHandle.TopAreanCode[] { TopAreanHandle.TopAreanCode.FINAL, TopAreanHandle.TopAreanCode.F1256, TopAreanHandle.TopAreanCode.F3478, TopAreanHandle.TopAreanCode.F12, TopAreanHandle.TopAreanCode.F34,
                    TopAreanHandle.TopAreanCode.F56, TopAreanHandle.TopAreanCode.F78 };
            for (TopAreanHandle.TopAreanCode finalCode : finalCodes) {
                TopAreanStructure topAreanStructure = mapStructure.get(finalCode);
                if (topAreanStructure == null) {
                    continue;
                }
                response.addFinalInfo(packFinalInfo(playerCode, topAreanStructure,group));
            }
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private ArenaMessage.TopArenaInfo packFinalInfo(String playerCode, TopAreanStructure topAreanStructure, int group) {
        ArenaMessage.TopArenaInfo.Builder infobuilder = ArenaMessage.TopArenaInfo.newBuilder();
        infobuilder.setFinalCode(yxy.game.pm2.message.ArenaMessage.TopArenaCode.forNumber(topAreanStructure.getFinalCode().getNumber()));
        {
            TopAreanPlayer finalPlayer = topAreanStructure.getPlayer1();
            ArenaMessage.TopArenaFighter.Builder builder = ArenaMessage.TopArenaFighter.newBuilder();
            builder.setFans(finalPlayer.getFans().size());
            builder.setLiked(finalPlayer.getFans().contains(playerCode));
            builder.setPlayerInfo(new PlayerHandle().playerSimpleInfo(finalPlayer.getPlayerCode()));
            builder.setGroup(group);
            infobuilder.addPlayers(builder);
        }
        {
            TopAreanPlayer finalPlayer = topAreanStructure.getPlayer2();
            ArenaMessage.TopArenaFighter.Builder builder = ArenaMessage.TopArenaFighter.newBuilder();
            builder.setFans(finalPlayer.getFans().size());
            builder.setLiked(finalPlayer.getFans().contains(playerCode));
            builder.setPlayerInfo(new PlayerHandle().playerSimpleInfo(finalPlayer.getPlayerCode()));
            builder.setGroup(group);
            infobuilder.addPlayers(builder);
        }
        if (topAreanStructure.getWinner() != null) {
            int battleResult = topAreanStructure.getWinner().getPlayerCode().equals(topAreanStructure.getPlayer1().getPlayerCode()) ? 1 : 2;
            infobuilder.setRecord(topAreanStructure.getRecord());
            infobuilder.setBattleResult(battleResult);
        }

        return infobuilder.build();
    }

    private void send(MessageSource messageSource, ArenaMessage.TopArenaInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
